﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenChart.Entities;

namespace WinForm

{
    public partial class FormLineChart : Form
    {
        public Timer timer1 { get; set; }//定时器，用来作为更新图标动态生成用

        public FormLineChart()
        {
            InitializeComponent();
            this.AddTestData();
            this.InitChart();
            // 启动定时器
            timer1 = new Timer();
            timer1.Interval = 1000;
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Start();
        }

        private void AddTestData()
        {
            DataRow newRow = null;
            for (int i = 0; i < 30; i++)
            {
                newRow = this.dsTest1.Tables[0].NewRow();
                newRow["F_X"] = DateTime.Now.AddSeconds(i);
                newRow["F_Y1"] = i * i;
                newRow["F_Y2"] = 2 * i * i + 10;
                newRow["F_Y3"] = 10 * i;
                newRow["F_Y4"] = 20 * i;
                this.dsTest1.Tables[0].Rows.Add(newRow);
            }
        }

        private void InitChart()
        {
            this.chart1.BackColor = Color.White;

            this.chart1.ChartArea.ChartType = Enums.ChartType.Line;
            this.chart1.ChartArea.Data = this.dsTest1.Tables[0];
            this.chart1.ChartArea.XValueMember = "F_X";

            StringInfo chartTitle = new StringInfo();
            chartTitle.Value = "光栅折线图";
            chartTitle.Color = Color.DarkBlue;
            chartTitle.Size = 15;
            this.chart1.ChartArea.ChartTitle = chartTitle;

            this.chart1.ChartArea.AxisLineColor = Color.DarkSeaGreen;
            this.chart1.ChartArea.AxisLineWidth = 2;

            PartingLine line = new PartingLine();
            line.YValue = 500;
            line.LineColor = Color.DarkGreen;
            this.chart1.ChartArea.PartingLines.Add(line);

            StringInfo axisXLable = new StringInfo();
            axisXLable.Size = 12;
            axisXLable.Color = Color.Silver;
            axisXLable.Value = "时间";
            this.chart1.ChartArea.AxisX.Lable = axisXLable;

            this.chart1.ChartArea.AxisX.Interval = 1;
            this.chart1.ChartArea.AxisX.IntervalType = Enums.InervalType.Seconds;
            this.chart1.ChartArea.AxisX.TimeFormat = "HH:mm:ss";
            this.chart1.ChartArea.AxisX.Grid.LineColor = Color.RosyBrown;
            this.chart1.ChartArea.AxisX.Grid.LineStyle = Enums.LineStyle.DashDot;
            this.chart1.ChartArea.AxisX.ShowGrid = true;

            StringInfo axisYLable = new StringInfo();
            axisYLable.Size = 12;
            axisYLable.Color = Color.Silver;
            axisYLable.Value = "数据";
            this.chart1.ChartArea.AxisY1.Lable = axisYLable;

            this.chart1.ChartArea.AxisY1.Interval = 100;
            this.chart1.ChartArea.AxisY1.IntervalType = Enums.InervalType.Number;
            this.chart1.ChartArea.AxisY1.Grid.LineColor = Color.SaddleBrown;
            this.chart1.ChartArea.AxisY1.Grid.LineStyle = Enums.LineStyle.DashDot;
            this.chart1.ChartArea.AxisY1.ShowGrid = true;

            Series series1 = new Series();
            series1.Name = "SeriesOne";
            series1.Color = Color.Red;
            series1.Width = 1;
            series1.YValueMember = "F_Y1";
            this.chart1.ChartArea.Series.Add(series1);

            Series series2 = new Series();
            series2.Name = "SeriesTwo";
            series2.Color = Color.RoyalBlue;
            series2.Width = 1;
            series2.YValueMember = "F_Y2";
            this.chart1.ChartArea.Series.Add(series2);

            Series series3 = new Series();
            series3.Name = "SeriesThree";
            series3.Color = Color.SeaGreen;
            series3.Width = 1;
            series3.YValueMember = "F_Y3";
            this.chart1.ChartArea.Series.Add(series3);

            Series series4 = new Series();
            series4.Name = "SeriesFour";
            series4.Color = Color.PaleGreen;
            series4.Width = 1;
            series4.YValueMember = "F_Y4";
            this.chart1.ChartArea.Series.Add(series4);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // 随机生成一个数据
            DataRow newRow = this.dsTest1.Tables[0].NewRow();
            newRow["F_X"] = DateTime.Now.AddSeconds(30);
            // 随机生成一个数据
            double value1 = (double)(Math.Round((decimal)(new Random().NextDouble() * 1000), 5));
            newRow["F_Y1"] = value1;
            double value2 = (double)(Math.Round((decimal)(new Random().NextDouble() * 1000), 5));
            newRow["F_Y2"] = value2;
            double value3 = (double)(Math.Round((decimal)(new Random().NextDouble() * 1000), 5));
            newRow["F_Y3"] = value3;
            double value4 = (double)(Math.Round((decimal)(new Random().NextDouble() * 1000), 5));
            newRow["F_Y4"] = value4;
            this.dsTest1.Tables[0].Rows.Add(newRow);
            if(this.dsTest1.Tables[0].Rows.Count > 30)
            {
                //this.dsTest1.Tables[0].Rows[0].Delete();
                this.dsTest1.Tables[0].Rows.RemoveAt(0);
            }

            this.chart1.Refresh();
        }
    }
}
